package top.tangyh.lamp.care.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.tangyh.lamp.care.mapper.BHl7EventMapper;
import top.tangyh.lamp.care.mapper.BPatientMapper;
import top.tangyh.lamp.care.mapper.LeavePatientsMapper;
import top.tangyh.lamp.care.service.LeavePatientsService;
import top.tangyh.basic.base.service.impl.SuperServiceImpl;
import top.tangyh.lamp.care.manager.LeavePatientsManager;
import top.tangyh.lamp.care.entity.LeavePatients;
import top.tangyh.lamp.care.vo.query.CancelPatient;
import top.tangyh.lamp.care.vo.save.LeavePatientsSaveVO;
import top.tangyh.lamp.care.vo.update.LeavePatientsUpdateVO;
import top.tangyh.lamp.care.vo.result.LeavePatientsResultVO;
import top.tangyh.lamp.care.vo.query.LeavePatientsPageQuery;
import top.tangyh.lamp.common.constant.CommonConstant;

import java.util.List;

/**
 * <p>
 * 业务实现类
 * 已撤销病人
 * </p>
 *
 * @author zqb
 * @date 2024-07-23 09:08:13
 * @create [2024-07-23 09:08:13] [zqb] [代码生成器生成]
 */
@Slf4j
@RequiredArgsConstructor
@Service
@Transactional(readOnly = true)
public class LeavePatientsServiceImpl extends SuperServiceImpl<LeavePatientsManager, Long, LeavePatients> implements LeavePatientsService {

    private final LeavePatientsMapper leavePatientsMapper;

    private final BPatientMapper bPatientMapper;

    private final BHl7EventMapper bHl7EventMapper;


    @Override
    public List<LeavePatients> selectLeavePatients() {
        LambdaQueryWrapper<LeavePatients> wrapperQuery = new LambdaQueryWrapper<>();
        wrapperQuery.eq(LeavePatients::getIsCancel, 0)
                .eq(LeavePatients::getIsDeleted, CommonConstant.NO_NUMBER);
        List<LeavePatients> list = this.list(wrapperQuery);
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int cancelPatients(CancelPatient cancelPatient) {
        //修改 为已撤销
        LambdaUpdateWrapper<LeavePatients> updateQueryWrapper = Wrappers.<LeavePatients>lambdaUpdate()
                .eq(LeavePatients::getId, cancelPatient.getId())
                .eq(LeavePatients::getIsDeleted,CommonConstant.NO_NUMBER)
                .set(LeavePatients::getIsCancel, 1);
        Integer rows = leavePatientsMapper.update(null, updateQueryWrapper);

        //修改离院状态为否
        bPatientMapper.updateLiYuanStatus(cancelPatient.getHospitalNumber(), 0);

        //删除出科事件
        int rowsEvent = bHl7EventMapper.deleteById(cancelPatient.getEventId());

        return rows;
    }
}


